<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('合同管理列表')" />
</head>
<body class="gray-bg">
     <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="formId">
                    <div class="select-list">
                        <ul>
                            <li>
                                <label>合同编号：</label>
                                <input type="text" name="contractCode"/>
                            </li>
                            <li>
                                <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                                <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>

            <div class="btn-group-sm" id="toolbar" role="group">
                <a class="btn btn-success" onclick="$.operate.add()" >
                    <i class="fa fa-plus"></i> 添加
                </a>
                <a class="btn btn-primary single disabled" onclick="edit()" >
                    <i class="fa fa-edit"></i> 修改
                </a>
                <a class="btn btn-info single disabled" onclick="$.operate.detail()">
                    <i class="fa fa-search"></i> 详细
                </a>
                <a class="btn btn-danger multiple disabled" onclick="removeAll()" >
                    <i class="fa fa-remove"></i> 删除
                </a>
                <a class="btn btn-warning hidden" onclick="$.table.exportExcel()" >
                    <i class="fa fa-download"></i> 导出
                </a>
                <a class="btn btn-primary single disabled" onclick="onCheck(null,null)" >
                    <i class="fa fa-edit"></i> 审核
                </a>
            </div>

            <div class="modal inmodal fade" id="myModal5" tabindex="-1" role="dialog" aria-hidden="true">
                <div class="modal-dialog modal-lg">
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span>
                            </button>
                            <h4 class="modal-title">审核合同</h4>
                        </div>
                        <div class="modal-body">
                            <div class="row">

                            </div>
                        </div>

                        <div class="modal-footer">
                            <button type="button" class="btn btn-white" data-dismiss="modal">关闭</button>
                            <button type="button" class="btn btn-primary">保存</button>
                        </div>
                    </div>
                </div>
            </div>

            <div class="col-sm-12 select-table table-striped">
                <table id="bootstrap-table"></table>
            </div>
        </div>
    </div>
    <th:block th:include="include :: footer" />
    <script th:inline="javascript">
        var editFlag = [[${@permission.hasPermi('contract:contract:edit')}]];
        var removeFlag = [[${@permission.hasPermi('contract:contract:remove')}]];
        let statusDatas = [[${@dict.getType('b_contrack_status')}]];
        let contractType=[[${contractType}]];//合同类型
        let roleFlag =[[${roleFlag}]];


        //判断是否是销售合同
        function isSellContract(){
            if(contractType==contractTypeObj.sell){
                return true;
            }else{
                return false;
            }
        }
        var huodan=isSellContract()?'销货单':'提货单';
        var ku=isSellContract()?'出库':'入库';

        var prefix = ctx + "contract/contract";

        $(function() {
            var options = {
                url: prefix + "/list?contractType="+contractType+"&roleFlag="+roleFlag,
                createUrl: prefix + "/add?contractType="+contractType,
                updateUrl: prefix + "/edit/{id}",
                removeUrl: prefix + "/remove",
                exportUrl: prefix + "/export",
                detailUrl: prefix + "/detail/{id}",
                sortName: "contractId",
                sortOrder:"desc",
                modalName: isSellContract()?"销售合同":"采购合同",
                columns: [{
                    checkbox: true
                },
                {
                    field: 'contractId',
                    title: '合同ID',
                    visible: false
                },
                    {
                        field: 'buyDeptId',
                        title: '购买部门ID',
                        visible: false,
                    },
                {
                    field: 'contractCode',
                    title: isSellContract()?'销售合同编号':'采购合同编号',
                    formatter: function(value, row, index) {
                        return value;
                    }
                },
                {
                    field: 'name',
                    title: isSellContract()?'销售合同名称':'采购合同名称'
                },
                    {
                        field: 'totalMoney',
                        title: isSellContract()?'销售合同金额':'采购合同金额'
                    },
                {
                    field: 'orderMoney',
                    title: isSellContract()?'已出库总金额':'已入库总金额'
                },
                    {
                        field: 'useMoney',
                        title: isSellContract()?'已出库对账金额':'已入库对账金额'
                    },
                {
                    field: 'payTotalMoney',
                    title: isSellContract()?'已收款金额（元）':'已付款金额(元)',
                    visible: true
                },
                {
                    field: 'startTime',
                    title: '有效开始时间'
                },
                {
                    field: 'endTime',
                    title: '有效结束时间'
                },
                {
                    field: 'status',
                    title: '状态',
                    visible: editFlag?false:true,
                    formatter: function(value, row, index) {
                        if(value==0) {
                            return $.common.sprintf("<span class='%s'>%s</span>", "badge badge-warning", "待审核");
                        }
                        return $.table.selectDictLabel(statusDatas, value);
                    }
                },
                {
                    field: 'remark',
                    title: '备注'
                },
                {
                    field: 'createTime',
                    title: '创建时间'
                },
                    {
                        title: '合同履约',
                        align: 'center',
                        formatter: function(value, row, index) {
                            var actions = [];
                            if(row.status==0){
                                actions.push('<a class="btn btn-dark btn-xs "  href="javascript:void(0)" onclick="onCheck(' + row.contractId + ','+row.status+')"><i class="fa fa-search1"></i>待审核</a> ');
                            }
                            if(row.status==1){
                                actions.push('<a class="btn btn-dark btn-xs "  href="javascript:void(0)" onclick="onCheck(' + row.contractId + ','+row.status+')"><i class="fa fa-search1"></i>查看审核意见</a> ');

                               // actions.push('<a class="btn btn-dark btn-xs "  href="javascript:void(0)" onclick="openContractOrders(' + row.contractId + ','+row.buyDeptId+')"><i class="fa fa-search1"></i>'+huodan+'</a> ');
                                actions.push('<a class="btn btn-dark btn-xs " href="javascript:void(0)" onclick="openContractOrderDetail(\'' + row.contractId + '\')"><i class="fa fa-search1"></i>'+ku+'</a> ');
                                actions.push('<a class="btn btn-dark btn-xs " href="javascript:void(0)" onclick="openContractAccount(\'' + row.contractId + '\')"><i class="fa fa-search1"></i>对账单</a> ');

                            }
                            if(row.status == 2){
                                actions.push('<a class="btn btn-dark btn-xs "  href="javascript:void(0)" onclick="onCheck(' + row.contractId + ','+row.status+')"><i class="fa fa-search1"></i>查看审核意见</a> ');
                            }


                            return actions.join('');
                        }
                    }]
            };
            $.table.init(options);
        });

        function edit(){
            var data = $("#bootstrap-table").bootstrapTable('getSelections');
            contractId= data[0].contractId;
            let  status=data[0].status;
            if(status==1){
                $.modal.alert("已经审核通过，不能再修改");
                return
            }
            $.operate.edit()
        }

        function removeAll(){
            var data = $("#bootstrap-table").bootstrapTable('getSelections');
            contractId= data[0].contractId;
            let  status=data[0].status;
            if(status==1){
                $.modal.alert("已经审核通过，不能再删除");
                return
            }
            $.operate.removeAll()
        }

        //审核
        function onCheck(contractId,status){
            //$("#myModal5").click();
           // $('#myModal5').modal('toggle');
            if(isEmptyString(contractId)){
                var data = $("#bootstrap-table").bootstrapTable('getSelections');
                contractId= data[0].contractId;
                status=data[0].status;
                //handStatus(status)
            }
            openWin("审核合同",ctx+"contract/contractCheck?contractId="+contractId);
            // if(status==1){
            //     $.modal.alert("已经审核通过，不能再审核");  return;
            // }else{
            //     open("审核合同",ctx+"contract/contractCheck?contractId="+contractId);
            // }

        }


        //订单
        function openContractOrders(id,buyDeptId){
            openWin(huodan,ctx+"order/order?contractId="+id+"&contractType="+contractType+"&supplierId="+buyDeptId);
        }

        function openContractOrderDetail(id){
            if(isSellContract()){
                //出库
                $.modal.open("出库",ctx+"warehouse/orderDetail/sellOrderDetail?contractId="+id+"&roleFlag="+roleFlag,1200,$(window).height() - 50,function (index,layero){
                    var wind = layero.find('iframe')[0].contentWindow;
                    var rowsjSON = wind.getAllStoreData();
                    if(rowsjSON.length>0){
                        var contractId=wind.getContractId();
                        var data = {"params[list]":rowsjSON,"contractId":contractId};
                        $.operate.save(ctx+"warehouse/sellStoreDetail/add", data,function (result){
                            if (result.code == web_status.SUCCESS) {
                                $.modal.msg("入库成功！");
                                $.modal.close(index);
                            }else{
                                $.modal.alertError(result.msg);
                            }
                        });
                    }
                });
            }else{
                //入库
                $.modal.open("入库",ctx+"warehouse/orderDetail/mallOrderDetail?contractId="+id+"&roleFlag="+roleFlag,1200,$(window).height() - 50,function (index,layero){
                    var wind = layero.find('iframe')[0].contentWindow;
                    var rowsjSON = wind.getAllStoreData();
                    if(rowsjSON.length>0){
                        var contractId=wind.getContractId();
                        var data = {"params[list]":rowsjSON,"contractId":contractId};
                        $.operate.save(ctx+"warehouse/mallStoreDetail/add", data,function (result){
                            if (result.code == web_status.SUCCESS) {
                                $.modal.msg("入库成功！");
                                $.modal.close(index);
                            }else{
                                $.modal.alertError(result.msg);
                            }
                        });
                    }
                });
            }

        }
        function openContractAccount(id){
            openWin('新增对账单',ctx+"account/account?type="+contractType+"&contractId="+id);
        }


    </script>
</body>
</html>
